package org.dromara.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.ShoppingCart;
import org.dromara.business.domain.vo.GoodsVo;
import org.dromara.business.domain.vo.ShoppingCartVo;
import org.dromara.business.mapper.ShoppingCartMapper;
import org.dromara.business.service.GoodsService;
import org.dromara.business.service.ShoppingCartService;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车Service业务层处理
 *
 * @author zlj
 * @date 2024-08-10
 */
@RequiredArgsConstructor
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    private final ShoppingCartMapper baseMapper;
    private final GoodsService mallService;
    private final ISysUserService userService;

    /**
     * 查询购物车列表
     */
    @Override
    public List<ShoppingCartVo> queryList() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getCreateBy, LoginHelper.getUserId());
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public boolean add(Long goodsMallId, Integer isAdd) {
        ShoppingCart shoppingCart = this.lambdaQuery()
            .eq(ShoppingCart::getGoodsId, goodsMallId)
            .eq(ShoppingCart::getCreateBy, LoginHelper.getUserId()).one();
        //查询商品
        GoodsVo goodsMallVo = mallService.queryById(goodsMallId);
        mallService.setPrice(List.of(goodsMallVo));
        if (ObjectUtil.isNull(shoppingCart)) {
            ShoppingCart add = new ShoppingCart();
            add.setName(goodsMallVo.getName());
            add.setUrl(Long.parseLong(goodsMallVo.getUrl().split(",")[0]));
            add.setNum(BigDecimal.ONE);
            add.setGoodsId(goodsMallId);
            add.setPrice(goodsMallVo.getVipPrice());
            add.setAllPrice(goodsMallVo.getVipPrice());
            return this.save(add);
        }
        if (isAdd == 1) {
            shoppingCart.setNum(shoppingCart.getNum().add(BigDecimal.ONE));
            shoppingCart.setAllPrice(goodsMallVo.getVipPrice().multiply(shoppingCart.getNum()));
        } else {
            shoppingCart.setNum(shoppingCart.getNum().subtract(BigDecimal.ONE));
            if (shoppingCart.getNum().compareTo(BigDecimal.ZERO) <= 0) {
                return this.removeById(shoppingCart.getShoppingCartId());
            } else {
                shoppingCart.setAllPrice(goodsMallVo.getVipPrice().multiply(shoppingCart.getNum()));
            }
        }
        return this.updateById(shoppingCart);
    }


    @Override
    public boolean clear() {
        return this.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getCreateBy, LoginHelper.getUserId()));
    }

    @Override
    public boolean removeCartGoods(Long goodsId) {
        return this.lambdaUpdate().eq(ShoppingCart::getGoodsId, goodsId).eq(ShoppingCart::getCreateBy, LoginHelper.getUserId()).remove();
    }

}
